package com.zhoukai.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhoukai.constants.Constants;
import com.zhoukai.dto.ProducterDto;
import com.zhoukai.vo.DataGridView;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.config.annotation.Method;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import com.zhoukai.mapper.ProducterMapper;
import com.zhoukai.domain.Producter;
import com.zhoukai.service.ProducterService;

@DubboService(methods = {@Method(name = "addProducter",retries = 0)})
public class ProducterServiceImpl implements ProducterService {

    @Autowired
    private ProducterMapper producterMapper;

    @Override
    public DataGridView listProducterForPage(ProducterDto producterDto) {
//        Page<Producter> page=new Page<>(producterDto.getPageNum(),producterDto.getPageSize());
//        QueryWrapper<Producter> qw=new QueryWrapper<>();
//        qw.like(StringUtils.isNotBlank(producterDto.getProducterName()),Producter.COL_PRODUCTER_NAME,producterDto.getProducterName());
//        qw.like(StringUtils.isNotBlank(producterDto.getKeywords()),Producter.COL_PRODUCTER_NAME,producterDto.getKeywords());
//        qw.like(StringUtils.isNotBlank(producterDto.getProducterTel()),Producter.COL_PRODUCTER_TEL,producterDto.getProducterTel());
//        qw.eq(StringUtils.isNotBlank(producterDto.getStatus()),Producter.COL_STATUS,producterDto.getStatus());
//        Date beginTime = producterDto.getBeginTime();
//        Date endTime = producterDto.getEndTime();
//        qw.ge(beginTime!=null,Producter.COL_CREATE_TIME,beginTime);
//        qw.le(endTime!=null,Producter.COL_CREATE_TIME,endTime);
//        this.producterMapper.selectPage(page,qw);
//        return new DataGridView(page.getTotal(),page.getRecords());
        Page<Producter> page=new Page<>(producterDto.getPageNum(),producterDto.getPageSize());
        Date beginTime = producterDto.getBeginTime();
        Date endTime = producterDto.getEndTime();
        this.producterMapper.selectPage(page, Wrappers.lambdaQuery(Producter.class)
                .like(StringUtils.isNotBlank(producterDto.getProducterName()),Producter::getProducterName,producterDto.getProducterName())
                .like(StringUtils.isNotBlank(producterDto.getKeywords()),Producter::getKeywords,producterDto.getKeywords())
                .like(StringUtils.isNotBlank(producterDto.getProducterTel()),Producter::getProducterTel,producterDto.getProducterTel())
                .eq(StringUtils.isNotBlank(producterDto.getStatus()),Producter::getStatus,producterDto.getStatus())
                .ge(beginTime!=null,Producter::getCreateTime,beginTime)
                .le(endTime!=null,Producter::getCreateTime,endTime)
        );
        return new DataGridView(page.getTotal(),page.getRecords());
    }


    @Override
    public Producter getProducterById(Long producterId) {
        return this.producterMapper.selectById(producterId);
    }

    @Override
    public int addProducter(ProducterDto producterDto) {
        Producter producter=new Producter();
        BeanUtil.copyProperties(producterDto,producter);
        producter.setCreateTime(DateUtil.date());
        producter.setCreateBy(producterDto.getSimpleUser().getUserName());
        return this.producterMapper.insert(producter);

    }

    @Override
    public int updateProducter(ProducterDto producterDto) {
        Producter producter=new Producter();
        BeanUtil.copyProperties(producterDto,producter);
        producter.setUpdateTime(DateUtil.date());
        producter.setUpdateBy(producterDto.getSimpleUser().getUserName());
        return this.producterMapper.updateById(producter);

    }

    @Override
    public int deleteProducterByIds(Long[] producterIds) {
        List<Long> ids = Arrays.asList(producterIds);
        if(null!=ids&&!ids.isEmpty()){
            return this.producterMapper.deleteBatchIds(ids);
        }
        return 0;

    }

    @Override
    public List<Producter> selectAllProducter() {
        return this.producterMapper
                .selectList(Wrappers.lambdaQuery(Producter.class)
                .eq(Producter::getStatus, Constants.STATUS_TRUE)

        );
    }
}

